<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends \Common\Controller\CommonController {
    public function index(){

        $userid=session('userid');
        $user=M('account')->where(array('id' => $userid))->find();

        $zt_user=M('account')->where(array('parid' => $userid,'status' => 1))->select();
        $user['ztnum']=count($zt_user)+0;//直推人数

        if($user['ztnum']>0){

            session('z_usernum',$user['ztnum']);

            $sqlw='1=1 and status=1 and';

            for($i=0;$i<count($zt_user);$i++)
            {
                if($i==0){
                    $sqlw.=' (';
                }
                $id1 = $zt_user[$i]['id'];
                $sqlw.=' parid='.$id1;

                if($i<count($zt_user)-1){
                    $sqlw.=' or ';
                }else{
                    $sqlw.=') ';
                }

            }
            
            for($a=0;;$a++)
            {

                $sqlw=$this->next($sqlw,$a);

                if(empty($sqlw)){
                    break;
                }

            }
            $user['z_usernum']=$_SESSION['z_usernum'];//组织总人数
        }else{
            $user['z_usernum']=0;//组织人数
        }

        $z_jj=M('reward')->where(array('uid' => $userid))->sum('money');//总奖金
        $zhijj=M('reward')->where(array('uid' => $userid,'typemoney' => 2))->sum('money');//直推奖金
        if($zhijj){
            $user['zhijj']=$zhijj;//直推奖金
        }else{
            $user['zhijj']=0;//直推奖金
        }
        
        $user['z_fh']=$z_jj-$zhijj+0;//总分红

// echo strtotime('2017-3-14');

        $BeginDate=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));//当月的第一天
        $EndDate=date('Y-m-d 23:59:59', strtotime("$BeginDate +1 month -1 day"));//当月的最后一天
        // echo $BeginDate.'<br>';
        // echo $EndDate;
        $day1=strtotime($BeginDate);
        $day2=strtotime($EndDate);
        $map['times_c'] = array(array('egt',$day1),array('elt',$day2));
        $map['uid'] = $userid;
        $map['typemoney'] = 1;
        $jj1=M('reward')->where($map)->sum('money');

        $map['typemoney'] = 3;
        $jj3=M('reward')->where($map)->sum('money');

        $user['yue_fh']=$jj1+$jj3+0;//本月分红

        // $ktx_fh=$user['z_fh']-$user['yue_fh'];

        // session('ktx_fh',$ktx_fh);//可提现分红

        $zt_price=M('account')->where(array('parid' => $userid,'status' => 1))->sum('investmoney');

        if($zt_price){
            $user['zt_price']=$zt_price;//推荐金额
        }else{
            $user['zt_price']=0;//推荐金额
        }

        


        $ks=date('Y-m-d 00:00:00', time());//当天的开始时间
        $js=date('Y-m-d 23:59:59', time());//当天的结束时间
        $kstime=strtotime($ks);
        $jstime=strtotime($js);
        $map['times_c'] = array(array('egt',$kstime),array('elt',$jstime));
        // $map['uid'] = $userid;
        $map['typemoney'] = 1;
        $fh1=M('reward')->where($map)->sum('money');

        $map['typemoney'] = 3;
        $fh3=M('reward')->where($map)->sum('money');

        $user['tian_fh']=$fh1+$fh3+0;//今日分红
        

        $this->assign("user",$user);


        $list = M('news')->select();

        $this->assign('list', $list); // 賦值數據集

        $domain=$_SERVER['HTTP_HOST'];
        $this->assign('domain',$domain);

        $today = date('Y-m-d', time());
        $tags='-';
        $today = explode($tags,$today);
        $hao = abs($today[2]);//当前时间的号数（今天是几号）
        // echo $hao;
        // $ymd=abs($today[0]).$tags.abs($today[1]).$tags.'1';
        // $ymd=strtotime($ymd);
        // echo $ymd.'<br>';
        // $ymd1='2017-3-1 00:00:00';
        // $ymd1=strtotime($ymd1);
        // echo $ymd1;
        for($t=1;$t<=$hao;$t++){

            if($t<10){
                $t='0'.$t;
            }

            $array1[$t]="'".$t.'日'."'";
            $ymd=abs($today[0]).$tags.abs($today[1]).$tags.$t;
            $ymd=strtotime($ymd);
            // echo $ymd.'<br>';
            $jtfh=M('reward')->where(array('uid' => $userid,'times_c' => $ymd,'typemoney' => 1))->sum('money');//静态分红
            if(!$jtfh){
                $jtfh=0;
            }
            $array2[$t]=$jtfh;
            $dtfh=M('reward')->where(array('uid' => $userid,'times_c' => $ymd,'typemoney' => 3))->sum('money');//动态分红
            if(!$dtfh){
                $dtfh=0;
            }
            $array3[$t]=$dtfh;
  
        }

        $string1=implode(',',$array1);//数组转化成字符串用空格''连接
        $string1='['.$string1.']';//数组转化成字符串用空格''连接
        $string2=implode(',',$array2);//数组转化成字符串用空格''连接
        $string2='['.$string2.']';//数组转化成字符串用空格''连接
        $string3=implode(',',$array3);//数组转化成字符串用空格''连接
        $string3='['.$string3.']';//数组转化成字符串用空格''连接
        
        $this->assign('string1', $string1);
        $this->assign('string2', $string2);
        $this->assign('string3', $string3);

        $yue = abs($today[1]);//当前时间的月份

        for($m=1;$m<=$yue;$m++){

            if($m<10){
                $m='0'.$m;
            }

            $array4[$m]="'".abs($today[0]).$m."'";

            $m_beg=abs($today[0]).$tags.$m.$tags.'01';
            $m_end=date('Y-m-d 23:59:59', strtotime("$m_beg +1 month -1 day"));//当月的最后一天
            $m_beg=strtotime($m_beg);
            $m_end=strtotime($m_end);
            // echo $m_beg.'<br>';
            // echo $m_end.'<br>';
            $mapm['times_c'] = array(array('egt',$m_beg),array('elt',$m_end));
            $mapm['uid'] = $userid;
            $mapm['typemoney'] = 1;
            $jt=M('reward')->where($mapm)->sum('money');//月静态分红
            if(!$jt){
                $jt=0;
            }
            $array5[$m]=$jt;

            $mapm['typemoney'] = 3;
            $dt=M('reward')->where($mapm)->sum('money');//月动态分红
            if(!$dt){
                $dt=0;
            }
            $array6[$m]=$dt;
    
        }
        
        $string4=implode(',',$array4);//数组转化成字符串用空格''连接
        $string4='['.$string4.']';//数组转化成字符串用空格''连接
        $string5=implode(',',$array5);//数组转化成字符串用空格''连接
        $string5='['.$string5.']';//数组转化成字符串用空格''连接
        $string6=implode(',',$array6);//数组转化成字符串用空格''连接
        $string6='['.$string6.']';//数组转化成字符串用空格''连接
        
        $this->assign('string4', $string4);
        $this->assign('string5', $string5);
        $this->assign('string6', $string6);
        

        
        $this->display();
     }

     //下一代
    private function next($sqlw,$a)
    {
        $z_par=M('account')->where($sqlw)->select();
        $sqlw2='1=1 and status=1 and';
        $z_parnum = count($z_par);
        $z_usernum=$_SESSION['z_usernum']+$z_parnum;
        session('z_usernum',$z_usernum);

        if($z_parnum==0){

            return;

        }

        for($j=0;$j<$z_parnum;$j++){

            if($j==0){
                $sqlw2.=' (';
            }

            $id1 = $z_par[$j]['id'];
            $sqlw2.=' parid='.$id1;

            if($j<$z_parnum-1){
                $sqlw2.=' or ';
            }else{
                $sqlw2.=') ';
            }

        }

        return $sqlw2;
    }

    //生成二维码
    public function qrcode($level=3,$size=4)
    {
        $u=I('get.url');
        $url='http://'.$u;
        Vendor('phpqrcode');//Tp/Library/Vendor/phpqrcode.php
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        //生成二维码图片
        $object = new \QRcode();
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }

    public function hyzl(){


        $this->display();
    }

    public function xgzl(){


        $this->display();
    }

    public function zl_xg(){

        $data['cart_id'] = I('post.sfz');
        if(empty($data['cart_id'])){

            die("<script>alert('请填写身份证号码!');history.back(-1);</script>");

            // echo"<script>alert('请填写身份证号码!');location.href='/user/xgzl.html'</script>";
            // die();

        }
        $data['bank_name'] = I('post.khyh');
        if(empty($data['bank_name'])){

            die("<script>alert('请填写开户银行!');history.back(-1);</script>");

        }
        $data['bank_username'] = I('post.khxm');
        if(empty($data['bank_username'])){

            die("<script>alert('请填写开户姓名!');history.back(-1);</script>");

        }
        $data['bank_num'] = I('post.yhkh');
        if(empty($data['bank_num'])){

            die("<script>alert('请填写银行卡号!');history.back(-1);</script>");

        }
        if (!preg_match ( ' /^(\d{16}|\d{19})$/',$data['bank_num'] )){

            die("<script>alert('银行卡号格式不对!');history.back(-1);</script>");
        }
        $data['bank_zhi'] = I('post.zhhxx');
        if(empty($data['bank_zhi'])){

            die("<script>alert('请填写支行信息!');history.back(-1);</script>");

        }
        $data['province'] = I('post.dzhi');
        if(empty($data['province'])){

            die("<script>alert('请填写省、市、区详细地址!');history.back(-1);</script>");

        }

        $reg=M('account')->where(array('id' => $_SESSION['userid']))->save($data);

        if($reg){
            echo"<script>alert('修改成功!');location.href='/user/xgzl.html'</script>";
            // $this->success("<script>alert('修改成功!');history.back(-1);</script>");
            // $this->success('修改成功!');
        }else{
            // $this->success('修改失败!');
            // $this->success("<script>alert('修改失败!');history.back(-1);</script>");
            echo"<script>alert('修改失败!');location.href='/user/xgzl.html'</script>";
        }
        
        
    }

    public function czmm(){


        $this->display();
    }

    public function xg_mm(){

        $id=I('post.id');
        if(!I('post.mm')){
            die("<script>alert('请输入旧".$id."级密码!');history.back(-1);</script>");
        }
        if(!I('post.xmm')){
            die("<script>alert('请输入新".$id."级密码!');history.back(-1);</script>");
        }
        if(!I('post.qrxmm')){
            die("<script>alert('请输入确认新".$id."级密码!');history.back(-1);</script>");
        }
        // $mm = md5(I('post.mm'));
        // $xmm = md5(I('post.xmm'));
        // $qrxmm = md5(I('post.qrxmm'));
        $mm = I('post.mm');
        $xmm = I('post.xmm');
        $qrxmm = I('post.qrxmm');

        if($xmm<>$qrxmm){
            die("<script>alert('新".$id."级密码和确认新".$id."级密码不一致!');history.back(-1);</script>");
        }

        $data['id']=session('userid');
        $user=M('account')->where($data)->find();

        if($id=="一"){
            // $data['firstpwd'] = $mm;
            $pwd = $user['firstpwd'];
            $map['firstpwd'] = $this->md5pw($xmm);
        }
        if($id=="二"){
            // $data['secpwd'] = $mm;
            $pwd = $user['secpwd'];
            $map['secpwd'] = $this->md5pw($xmm);
        }
        if($id=="三"){
            // $data['thirpwd'] = $mm;
            $pwd = $user['thirpwd'];
            $map['thirpwd'] = $this->md5pw($xmm);
        }

        

        if($this->_checkPwd($mm,$pwd)){

            $reg=M('account')->where(array('id' => $_SESSION['userid']))->save($map);

        }else{
            die("<script>alert('旧".$id."级密码错误!');history.back(-1);</script>");
        }

        // if(empty($user)){
        //     die("<script>alert('旧".$id."级密码错误!');history.back(-1);</script>");
        // }
        

        // $reg=M('account')->where(array('id' => $_SESSION['userid']))->save($map);

        if($reg){
            echo"<script>alert('修改成功!');location.href='/user/czmm.html'</script>";
            
        }else{
            
            echo"<script>alert('修改失败!');location.href='/user/czmm.html'</script>";
        }

        
    }

    public function hyft(){

        // $ftlb=M('redelivery')->where(array('userid' => $_SESSION['userid']))->select();

        $num = 5;//默认每頁顯示的記錄數

        $count = M('redelivery')->where(array('userid' => $_SESSION['userid']))->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('redelivery')->where(array('userid' => $_SESSION['userid']))->order('id DESC')->limit($p->firstRow, $p->listRows)->select();
        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數

        // $this->assign("list",$ftlb);


        $this->display();
    }

    public function ftform(){

        $price=I('post.price');
        if(!$price){
            die("<script>alert('请输入复投金额!');history.back(-1);</script>");
        }
        if($price<20){
            die("<script>alert('复投金额不能低于20,且必须是10的倍数!');history.back(-1);</script>");
        }
        if($price % 10 > 0){
            die("<script>alert('复投金额不能低于20,且必须是10的倍数!');history.back(-1);</script>");
        }
        if(!I('post.mm2')){
            die("<script>alert('请输入二级密码!');history.back(-1);</script>");
        }

        // $mm2=md5(I('post.mm2'));
        // $qianbao=I('post.qianbao');
        // $user=M('account')->where(array('id' => $_SESSION['userid'],'secpwd' => $mm2))->find();
        $mm2=I('post.mm2');
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();

        if($this->_checkPwd($mm2,$user['secpwd'])){

            $qianbao=I('post.qianbao');

        }else{
            die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        }

        // if(empty($user)){
        //     die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        // }

        if($qianbao==1){
            
            if($price>$user['cashmoney']){
                die("<script>alert('直接提现钱包余额不足,复投失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('cashmoney',$price);
            

        }
        if($qianbao==2){
            
            if($price>$user['yearmoney']){
                die("<script>alert('半年分红钱包余额不足,复投失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('yearmoney',$price);

        }

        $arr=array(
            'userid'=>$_SESSION['userid'],
            'ftmoney'=>$price,
            'ftqmoney'=>$user['investmoney'],
            'createtime'=>time(),
            'pay'=>$qianbao,
        );
        M('redelivery')->add($arr);//生成复投记录
        //增加投资金额
        M('account')->where(array('id' => $_SESSION['userid']))->setInc('investmoney',$price);

        $data=M('account')->where(array('id' => $_SESSION['userid']))->find();

        if($data['investmoney']>0 && $data['investmoney']<5000){
            $level = 1;
        }
        if($data['investmoney']>=5000 && $data['investmoney']<10000){
            $level = 3;
        }
        if($data['investmoney']>=10000 && $data['investmoney']<20000){
            $level = 4;
        }
        if($data['investmoney']>=20000){
            $level = 5;
        }
        // if($data['investmoney']>=30000){
        //     $data['level'] = 6;
        // }
        if($data['level']<$level){
            $map['level'] = $level;
            if($data['level']<2 && $level>1){

                $map['daitime'] = time();//成为代理的时间

            }
            M('account')->where(array('id' => $_SESSION['userid']))->save($map);//自动升级
            
        }

        if($reg){
            echo"<script>alert('复投成功!');location.href='/user/hyft.html'</script>";
            
        }else{
            
            echo"<script>alert('复投失败!');location.href='/user/hyft.html'</script>";
        }


        
    }

    public function sqtd(){

        $num = 5;//默认每頁顯示的記錄數

        $count = M('tuidan')->where(array('userid' => $_SESSION['userid']))->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('tuidan')->where(array('userid' => $_SESSION['userid']))->order('id DESC')->limit($p->firstRow, $p->listRows)->select();
        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數



        $this->display();
    }

    public function sq_td(){

        $data['bank_username'] = I('post.khxm');
        if(empty($data['bank_username'])){

            die("<script>alert('请填写开户姓名!');history.back(-1);</script>");

        }
        $data['bank_num'] = I('post.yhkh');
        if(empty($data['bank_num'])){

            die("<script>alert('请填写银行卡号!');history.back(-1);</script>");

        }
        if (!preg_match ( ' /^(\d{16}|\d{19})$/',$data['bank_num'] )){

            die("<script>alert('银行卡号格式不对!');history.back(-1);</script>");
        }
        $data['bank_name'] = I('post.khyh');
        if(empty($data['bank_name'])){

            die("<script>alert('请填写开户银行!');history.back(-1);</script>");

        }
        $data['bank_zhi'] = I('post.zhhxx');
        if(empty($data['bank_zhi'])){

            die("<script>alert('请填写支行信息!');history.back(-1);</script>");

        }

        $arr=array(
            'userid'=>$_SESSION['userid'],
            'price'=>$_SESSION['price'],
            'cashmoney'=>$_SESSION['ktx_fh'],
            'sqtime'=>time(),
            'bank_username'=>$data['bank_username'],
            'bank_num'=>$data['bank_num'],
            'bank_name'=>$data['bank_name'],
            'bank_zhi'=>$data['bank_zhi'],
            'status'=>1,
        );
        $reg=M('tuidan')->add($arr);//生成申请退单记录

        if($reg){
            echo"<script>alert('提交成功!');location.href='/user/sqtd.html'</script>";
            
        }else{
            
            echo"<script>alert('提交失败!');location.href='/user/sqtd.html'</script>";
        }


        
    }

    public function zchy(){

        if(I('get.tj')){
            $tjr = I('get.tj');
            $this->tjr=$tjr;
        }else{
            $tjr = $_SESSION['username'];
            $this->tjr=$tjr;
        }
        


        $this->display();
    }

    // public function register(){

    //     $tjr = I('get.tj');
    //     session('username',$tjr);
    //     session('logintime',time());//登录时间
    //     $this->tjr=$tjr;
        


    //     $this->display();
    // }

    public function add(){

        $yh=I('post.yh');
        if(!$yh){

            die("<script>alert('平台银行账户不能为空!');history.back(-1);</script>");

        }else{

            $bank=M('bank')->where(array('id' => $yh))->find();
            $data['pt_bank_name']=$bank['bank_name'];
            $data['pt_bank_username']=$bank['bank_username'];
            $data['pt_bank_num']=$bank['bank_num'];
            $data['pt_bank_zhi']=$bank['bank_zhi'];

        }

        // $data['parid'] = $_SESSION['userid'];//默认推荐人
        $data['status'] = 0;//默认未激活
        $data['zctime'] = time();//注册时间

        $data['account'] = I('post.account');
        if(empty($data['account'])){

            die("<script>alert('请填写会员账号!');history.back(-1);</script>");

        }
        $user=M('account')->where(array('account' => $data['account']))->find();
        if(!empty($user)){

            die("<script>alert('该会员账号已存在，请换一个试试!');history.back(-1);</script>");

        }
        $data['name'] = I('post.name');
        if(empty($data['name'])){

            die("<script>alert('请填写真实姓名!');history.back(-1);</script>");

        }
        $data['email'] = I('post.email');
        // if(empty($data['email'])){

        //     die("<script>alert('请填写邮箱!');history.back(-1);</script>");

        // }
        if(I('post.email')){
            // if (!preg_match ( ' /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$data['email'] )) {
            if (!preg_match ( ' /^\w+((-\w+)|(\.\w+))*\@[0-9a-zA-Z]+((-|\.)[0-9a-zA-Z]+)*\.[0-9a-zA-Z]+$/',I('post.email') )) {
                die("<script>alert('邮箱格式不对!');history.back(-1);</script>");
            }
        }
        $data['phone'] = I('post.phone');
        if(empty($data['phone'])){

            die("<script>alert('请填写手机号码!');history.back(-1);</script>");

        }
        if (I('post.phone')) {
            //正则表达式匹配
            if (!preg_match ( ' /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))\\d{8})$/',I('post.phone') )) {
                die("<script>alert('手机格式不对!');history.back(-1);</script>");
            }
        }
        $data['investmoney'] = I('post.investmoney');
        if(empty($data['investmoney'])){

            die("<script>alert('请填写投资金额!');history.back(-1);</script>");

        }
        // if(($data['investmoney']<1000) || ($data['investmoney']>30000) || ($data['investmoney'] % 1000 >0)){

        //     die("<script>alert('投资金额1000~30000,且必须是1000的倍数!');history.back(-1);</script>");

        // }
        $setting=M('setting')->where('id=1')->find();
        if(($data['investmoney']<$setting['min_money']) || ($data['investmoney']>$setting['max_money']) || ($data['investmoney'] % $setting['bei_money'] >0)){

            // die("<script>alert('投资金额1000~30000,且必须是1000的倍数!');history.back(-1);</script>");
            die("<script>alert('投资金额".$setting['min_money']."~".$setting['max_money'].",且必须是".$setting['bei_money']."的倍数!');history.back(-1);</script>");

        }
        if($data['investmoney']>0 && $data['investmoney']<5000){
            $data['level'] = 1;
        }
        if($data['investmoney']>=5000 && $data['investmoney']<10000){
            $data['level'] = 3;
        }
        if($data['investmoney']>=10000 && $data['investmoney']<20000){
            $data['level'] = 4;
        }
        if($data['investmoney']>=20000 && $data['investmoney']<=30000){
            $data['level'] = 5;
        }
        // if($data['investmoney']>=30000){
        //     $data['level'] = 6;
        // }
        // if($data['level']>1){
        //     $data['daitime'] = time();//成为代理的时间
        // }

        if(!I('post.firstpwd')){

            die("<script>alert('请填写一级密码!');history.back(-1);</script>");

        }
        if(!I('post.qr_firstpwd')){

            die("<script>alert('请填写确认一级密码!');history.back(-1);</script>");

        }
        $firstpwd = I('post.firstpwd');
        $qr_firstpwd = I('post.qr_firstpwd');

        if($firstpwd<>$qr_firstpwd){

            die("<script>alert('填写的一级密码和确认一级密码不一致!');history.back(-1);</script>");

        }
        $data['firstpwd'] = $this->md5pw(I('post.firstpwd'));

        
        if(!I('post.secpwd')){

            die("<script>alert('请填写二级密码!');history.back(-1);</script>");

        }
        if(!I('post.qr_secpwd')){

            die("<script>alert('请填写确认二级密码!');history.back(-1);</script>");

        }
        $secpwd = I('post.secpwd');
        $qr_secpwd = I('post.qr_secpwd');
        if($secpwd<>$qr_secpwd){

            die("<script>alert('填写的二级密码和确认二级密码不一致!');history.back(-1);</script>");

        }
        $data['secpwd'] = $this->md5pw(I('post.secpwd'));

        
        if(!I('post.thirpwd')){

            die("<script>alert('请填写三级密码!');history.back(-1);</script>");

        }
        if(!I('post.qr_thirpwd')){

            die("<script>alert('请填写确认三级密码!');history.back(-1);</script>");

        }
        $thirpwd = I('post.thirpwd');
        $qr_thirpwd = I('post.qr_thirpwd');
        if($thirpwd<>$qr_thirpwd){

            die("<script>alert('填写的三级密码和确认三级密码不一致!');history.back(-1);</script>");

        }
        $data['thirpwd'] = $this->md5pw(I('post.thirpwd'));

        $tjr = I('post.tjr');
        if(!I('post.tjr')){

            die("<script>alert('请填写推荐人账户名!');history.back(-1);</script>");

        }
        $tjruser=M('account')->where(array('account' => $tjr))->find();
        if(!$tjruser){

            die("<script>alert('填写的推荐人账户名不存在，请重新填写!');history.back(-1);</script>");

        }

        $data['parid'] = $tjruser['id'];
        $data['mbwt'] = I('post.mbwt');
        $data['mbdan'] = I('post.mbdan');
        if(!I('post.mbwt')){

            die("<script>alert('请选择密保问题!');history.back(-1);</script>");

        }
        if(!I('post.mbdan')){

            die("<script>alert('请填写密保答案!');history.back(-1);</script>");

        }
        if(!I('post.cart_id')){

            die("<script>alert('请填写身份证号!');history.back(-1);</script>");

        }
        $data['cart_id'] = I('post.cart_id');

        // if(!I('post.province')){

        //     die("<script>alert('请填写省·市·地区详细地址!');history.back(-1);</script>");

        // }
        $data['province'] = I('post.province');

        if(!I('post.bank_username')){

            die("<script>alert('请填写开户姓名!');history.back(-1);</script>");

        }
        $data['bank_username'] = I('post.bank_username');

        if(!I('post.bank_num')){

            die("<script>alert('请填写银行卡号!');history.back(-1);</script>");

        }
        $data['bank_num'] = I('post.bank_num');

        if(!I('post.bank_name')){

            die("<script>alert('请填写开户行!');history.back(-1);</script>");

        }
        $data['bank_name'] = I('post.bank_name');

        if(!I('post.bank_zhi')){

            die("<script>alert('请填写支行信息!');history.back(-1);</script>");

        }
        $data['bank_zhi'] = I('post.bank_zhi');

        $reg=M('account')->add($data);//生成新会员

        if($reg){
            echo"<script>alert('注册成功，等待激活!');location.href='/user/wdhy.html'</script>";
            
        }else{
            
            echo"<script>alert('注册失败!');location.href='/user/zchy.html'</script>";
        }


        
    }

    public function jbsj(){

        $userid=session('userid');

        $user=M('account')->where(array('id' => $userid))->find();

        $list=M('upgrade')->where(array('userid' => $userid))->select();

        for($i=0;$i<count($list);$i++){

            if($list[$i]['status']==0){

                $list[$i]['zt']='待审核';
        
            }
            if($list[$i]['status']==1){

                $list[$i]['zt']='已通过';
        
            }
            if($list[$i]['sjqlevel']==0){

                $list[$i]['sjq']='免费会员';
        
            }
            if($list[$i]['sjqlevel']==1){

                $list[$i]['sjq']='正式会员';
        
            }
            if($list[$i]['sjhlevel']==2){

                $list[$i]['sjh']='普通代理';
        
            }

        }

        $this->assign('list', $list);
        $this->assign('user', $user);


        $this->display();
    }

    public function sj_form(){

        $yh=I('post.yh');
        if(!$yh){

            die("<script>alert('平台银行账户不能为空!');history.back(-1);</script>");

        }else{

            $bank=M('bank')->where(array('id' => $yh))->find();
            // $map['bank_name']=$bank['bank_name'];
            // $map['bank_username']=$bank['bank_username'];
            // $map['bank_num']=$bank['bank_num'];
            // $map['bank_zhi']=$bank['bank_zhi'];

        }

        $price=I('post.price');
        if(!$price){
            die("<script>alert('请输入金额!');history.back(-1);</script>");
        }
        
        if(!I('post.mm2')){
            die("<script>alert('请输入二级密码!');history.back(-1);</script>");
        }

        // $mm2=md5(I('post.mm2'));
        
        // $user=M('account')->where(array('id' => $_SESSION['userid'],'secpwd' => $mm2))->find();

        // if(empty($user)){
        //     die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        // }
        $mm2=I('post.mm2');
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();

        if($this->_checkPwd($mm2,$user['secpwd'])){

            $arr=array(
                'userid'=>$_SESSION['userid'],
                'price'=>$price,
                'sjqlevel'=>$user['level'],
                'sjhlevel'=>2,
                'jstime'=>time(),
                'status'=>0,
                'bank_name'=>$bank['bank_name'],
                'bank_username'=>$bank['bank_username'],
                'bank_num'=>$bank['bank_num'],
                'bank_zhi'=>$bank['bank_zhi'],
            );
            $reg=M('upgrade')->add($arr);//生成代理申请记录

        }else{
            die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        }

        
        //增加投资金额
        // M('account')->where(array('id' => $_SESSION['userid']))->setInc('investmoney',$price);

        if($reg){
            echo"<script>alert('代理申请提交成功，等待审核!');location.href='/user/jbsj.html'</script>";
            
        }else{
            
            echo"<script>alert('代理申请提交失败!');location.href='/user/jbsj.html'</script>";
        }

    }

    public function wdhy(){

        $userid=session('userid');

        $zt_user=M('account')->where(array('parid' => $userid))->select();
        for($i=0;$i<count($zt_user);$i++){

            if($zt_user[$i]['status']==0){

                $zt_user[$i]['ztdj']='未激活';
                $zt_user[$i]['bg']='';

            }else{

                if($zt_user[$i]['level']==0){
                    $zt_user[$i]['ztdj']='免费会员';
                    $zt_user[$i]['bg']='bg-1';
                }
                if($zt_user[$i]['level']==1){
                    $zt_user[$i]['ztdj']='正式会员';
                    $zt_user[$i]['bg']='bg-2';
                }
                if($zt_user[$i]['level']==2){
                    $zt_user[$i]['ztdj']='普通代理';
                    $zt_user[$i]['bg']='bg-3';
                }
                if($zt_user[$i]['level']==3){
                    $zt_user[$i]['ztdj']='一级代理';
                    $zt_user[$i]['bg']='bg-4';
                }
                if($zt_user[$i]['level']==4){
                    $zt_user[$i]['ztdj']='二级代理';
                    $zt_user[$i]['bg']='bg-5';
                }
                if($zt_user[$i]['level']==5){
                    $zt_user[$i]['ztdj']='终极代理';
                    $zt_user[$i]['bg']='bg-6';
                }

            }

            

        }

        $this->assign('list', $zt_user);


        $this->display();
    }

    public function jjmx(){

        $map['uid']=$_SESSION['userid'];

        $type=I('get.type');

        if($type==1){
            $this->mr1 = "selected = 'selected'";
        }
        if($type==2){
            $this->mr2 = "selected = 'selected'";
        }
        if($type==3){
            $this->mr3 = "selected = 'selected'";
        }
        if($type<>0){
            $map['typemoney']=$type;
        }


        $kstime = I('get.b');
        $jstime = I('get.e');
        if($kstime<>''){
            $this->kstime=I('get.b');
        }
        if($jstime<>''){
            $this->jstime=I('get.e');
        }
        if ($kstime <> '' && $jstime <> '') {
// echo $kstime;die;
            $timeks=strtotime($kstime);
            $timejs=strtotime($jstime);
            
            // $map['createtime'] = array('between',"$timeks,$timejs");//提现时间
            $map['times_c'] = array(array('egt',$timeks),array('lt',$timejs));

        }elseif ($kstime <> '' && $jstime == '') {

            $timeks=strtotime($kstime);
            
            $map['times_c'] = array('egt',$timeks);//提现时间

        }elseif ($kstime == '' && $jstime <> '') {

            $timejs=strtotime($jstime);
            
            $map['times_c'] = array('lt',$timejs);//提现时间

        }

        $num = 5;//默认每頁顯示的記錄數

        $count = M('reward')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('reward')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();
        for($i=0;$i<count($list);$i++){
            $list[$i]['username']=$_SESSION['username'];
            if($list[$i]['typemoney']==2){

                $list[$i]['p5']=0;
                $list[$i]['p2']=0;
                $list[$i]['p3']=0;
                $list[$i]['zhi']=$list[$i]['money'];

            }else{

                $list[$i]['p5']=$list[$i]['money']*0.5;
                $list[$i]['p2']=$list[$i]['money']*0.2;
                $list[$i]['p3']=$list[$i]['money']*0.3;
                $list[$i]['zhi']=0;

            }
            

        }

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數

        $list2 = M('reward')->where($map)->select();
        for($j=0;$j<count($list2);$j++){
            
            if($list2[$j]['typemoney']==2){

                $list2[$j]['p5']=0;
                $list2[$j]['p2']=0;
                $list2[$j]['p3']=0;
                $list2[$j]['zhi']=$list2[$j]['money'];

            }else{

                $list2[$j]['p5']=$list2[$j]['money']*0.5;
                $list2[$j]['p2']=$list2[$j]['money']*0.2;
                $list2[$j]['p3']=$list2[$j]['money']*0.3;
                $list2[$j]['zhi']=0;

            }

            $z_money+=$list2[$j]['money'];
            $z_p5+=$list2[$j]['p5'];
            $z_p2+=$list2[$j]['p2'];
            $z_p3+=$list2[$j]['p3'];
            $z_zhi+=$list2[$j]['zhi'];

        }

        $this->assign('z_money', $z_money);
        $this->assign('z_p5', $z_p5);
        $this->assign('z_p2', $z_p2);
        $this->assign('z_p3', $z_p3);
        $this->assign('z_zhi', $z_zhi);


        $this->display();
    }

    public function cwmx(){


        $this->display();
    }

    public function wycz(){

        $map['uid']=$_SESSION['userid'];

        $num = 5;//默认每頁顯示的記錄數

        $count = M('invest')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('invest')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數


        $this->display();
    }

    public function tz_form(){

        $yh=I('post.yh');
        if(!$yh){

            die("<script>alert('平台银行账户不能为空!');history.back(-1);</script>");

        }else{

            $bank=M('bank')->where(array('id' => $yh))->find();
            // $map['bank_name']=$bank['bank_name'];
            // $map['bank_username']=$bank['bank_username'];
            // $map['bank_num']=$bank['bank_num'];
            // $map['bank_zhi']=$bank['bank_zhi'];

        }

        $setting=M('setting')->where('id=1')->find();

        $price=I('post.price');
        if(!$price){
            die("<script>alert('请输入投资金额!');history.back(-1);</script>");
        }
        // if(($price<1000) || ($price>30000) || ($price % 1000 >0)){
        if(($price<$setting['min_money']) || ($price>$setting['max_money']) || ($price % $setting['bei_money'] >0)){

            // die("<script>alert('投资金额1000~30000,且必须是1000的倍数!');history.back(-1);</script>");
            die("<script>alert('投资金额".$setting['min_money']."~".$setting['max_money'].",且必须是".$setting['bei_money']."的倍数!');history.back(-1);</script>");

        }
        if(!I('post.mm2')){
            die("<script>alert('请输入二级密码!');history.back(-1);</script>");
        }

        // $mm2=md5(I('post.mm2'));
        
        // $user=M('account')->where(array('id' => $_SESSION['userid'],'secpwd' => $mm2))->find();

        // if(empty($user)){
        //     die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        // }
        $mm2=I('post.mm2');
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();

        if($this->_checkPwd($mm2,$user['secpwd'])){

            $arr=array(
                'uid'=>$_SESSION['userid'],
                'inmoney'=>$price,
                'tzqmoney'=>$user['investmoney'],
                'times'=>time(),
                'status'=>0,
                'bank_name'=>$bank['bank_name'],
                'bank_username'=>$bank['bank_username'],
                'bank_num'=>$bank['bank_num'],
                'bank_zhi'=>$bank['bank_zhi'],
            );
            $reg=M('invest')->add($arr);//生成投资记录

        }else{
            die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        }

        
        //增加投资金额
        // M('account')->where(array('id' => $_SESSION['userid']))->setInc('investmoney',$price);

        if($reg){
            echo"<script>alert('投资申请提交成功，等待审核!');location.href='/user/wycz.html'</script>";
            
        }else{
            
            echo"<script>alert('投资申请提交失败!');location.href='/user/wycz.html'</script>";
        }


    }

    public function hbzh(){

        $map['userid']=$_SESSION['userid'];
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();
        $this->assign('user', $user);

        $num = 5;//默认每頁顯示的記錄數

        $count = M('transfer')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('transfer')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();
        for($i=0;$i<count($list);$i++){

            if($list[$i]['zhcpay']==1){
                $list[$i]['zhc']='直接提现钱包';
            }
            if($list[$i]['zhcpay']==2){
                $list[$i]['zhc']='季度分红钱包';
            }
            if($list[$i]['zhrpay']==3){
                $list[$i]['zhr']='半年分红钱包';
            }
            

        }

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數


        $this->display();
    }

    public function zh_form(){

        if(!I('post.price')){
            die("<script>alert('请输入转换金额!');history.back(-1);</script>");
        }
        $price=I('post.price');
        if(!I('post.mm2')){
            die("<script>alert('请输入二级密码!');history.back(-1);</script>");
        }
        // $mm2=md5(I('post.mm2'));
        
        // $user=M('account')->where(array('id' => $_SESSION['userid'],'secpwd' => $mm2))->find();

        // if(empty($user)){
        //     die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        // }
        $mm2=I('post.mm2');
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();

        if($this->_checkPwd($mm2,$user['secpwd'])){

            $zhc=I('post.zhc');

        }else{
            die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        }

        // $zhc=I('post.zhc');
        if($zhc==1){

            if($price>$user['cashmoney']){
                die("<script>alert('直接提现钱包余额不足,转账失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('cashmoney',$price);

        }
        if($zhc==2){
            
            if($price>$user['halfmoney']){
                die("<script>alert('季度分红钱包余额不足,转账失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('halfmoney',$price);

        }
        $zhr=I('post.zhr');
        if($zhr==3){
            //转账成功,转入半年分红钱包
            $reg2=M('account')->where(array('id' => $_SESSION['userid']))->setInc('yearmoney',$price);

        }

        $arr=array(
            'userid'=>$_SESSION['userid'],
            'zhcpay'=>$zhc,
            'zhrpay'=>$zhr,
            'zhbl'=>'1.0:1.0',
            'price'=>$price,
            'zhtime'=>time(),
        );
        M('transfer')->add($arr);//生成转账记录
        
        

        if($reg){
            echo"<script>alert('转账成功!');location.href='/user/hbzh.html'</script>";
            
        }else{
            
            echo"<script>alert('转账失败!');location.href='/user/hbzh.html'</script>";
        }


        
    }

    public function txgl(){

        $map['userid']=$_SESSION['userid'];
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();
        if($user['bank_name']<>'' && $user['bank_username']<>'' && $user['bank_num']<>'' && $user['bank_zhi']<>''){

            $arr['kong']=1;

            $this->kong=$arr;

        }
        $this->assign('user', $user);
        $bank=M('bank')->where(array('userid' => $_SESSION['userid']))->select();
        $this->assign('bank', $bank);
        // for($b=0;$b<count($bank);$b++){

        // }

        $num = 5;//默认每頁顯示的記錄數

        $count = M('tixing')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('tixing')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();
        for($i=0;$i<count($list);$i++){

            if($list[$i]['txpay']==1){
                $list[$i]['txpay']='直接提现钱包';
            }
            if($list[$i]['txpay']==2){
                $list[$i]['txpay']='季度分红钱包';
            }
            if($list[$i]['txpay']==3){
                $list[$i]['txpay']='半年分红钱包';
            }
            if($list[$i]['txpay']==4){
                $list[$i]['txpay']='直推奖钱包';
            }
            if($list[$i]['status']==0){
                $list[$i]['status']='等待审核';
            }
            if($list[$i]['status']==1){
                $list[$i]['status']='提现成功';
            }
            

        }

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數


        $this->display();
    }

    public function tx_form(){

        $map['userid']=$_SESSION['userid'];

        if(!I('post.yh')){
            die("<script>alert('您还没有提现银行账户，请添加银行账户!');location.href='/user/xyhk.html'</script>");
            // echo"<script>alert('您还没有提现银行账户，请添加银行账户!');location.href='/user/xyhk.html'</script>";
        }
        $yh=I('post.yh');
        if($yh=="mr"){

            $user=M('account')->where(array('id' => $_SESSION['userid']))->find();
            $map['bank_name']=$user['bank_name'];
            $map['bank_username']=$user['bank_username'];
            $map['bank_num']=$user['bank_num'];
            $map['bank_zhi']=$user['bank_zhi'];

        }else{

            $bank=M('bank')->where(array('id' => $yh))->find();
            $map['bank_name']=$bank['bank_name'];
            $map['bank_username']=$bank['bank_username'];
            $map['bank_num']=$bank['bank_num'];
            $map['bank_zhi']=$bank['bank_zhi'];

        }
        
        if(!I('post.price')){
            die("<script>alert('请输入提现金额!');history.back(-1);</script>");
        }
        $map['price']=I('post.price');
        // if(($map['price']<100) || ($map['price'] % 100 > 0)){
        //     die("<script>alert('提现金额100起提，且是100的倍数!');history.back(-1);</script>");
        // }

        if(!I('post.mm2')){
            die("<script>alert('请输入二级密码!');history.back(-1);</script>");
        }
        // $mm2=md5(I('post.mm2'));
        
        // $account=M('account')->where(array('id' => $_SESSION['userid'],'secpwd' => $mm2))->find();
        $account=M('account')->where(array('id' => $_SESSION['userid']))->find();

        // if(empty($account)){
        //     die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        // }
        $mm2=I('post.mm2');
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();

        if($this->_checkPwd($mm2,$user['secpwd'])){

            $map['txpay']=I('post.zhc');

        }else{
            die("<script>alert('二级密码输入错误!');history.back(-1);</script>");
        }




        $date1 = date('Y-m-d', $account['jhtime']);
        $date2 = date('Y-m-d', time());
        $tags='-';
        $date1 = explode($tags,$date1);
        $date2 = explode($tags,$date2);
        $monthNum = abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);//相差的月份数量
        $hao = abs($date2[2]);//当前时间的号数（今天是几号）
        // $date1 = "2003-08-11";
        // $date2 = "2008-11-06";
        // $monthNum = $this->getMonthNum( $date1 , $date2 );//相差的月份数量
        // echo $monthNum;

        $setting=M('setting')->where('id=1')->find();
        
        // $map['txpay']=I('post.zhc');
        if($map['txpay']==1){

            if(($map['price']<$setting['txmoney']) || ($map['price'] % $setting['txbei'] > 0)){
                die("<script>alert('直接提现钱包提现金额".$setting['txmoney']."起提，且是".$setting['txbei']."的倍数!');history.back(-1);</script>");
            }

            // if($monthNum<1 || $hao>5){
            //     die("<script>alert('直接提现钱包要注册激活之后下一个月开始，每个月的1~5号才能提现!');history.back(-1);</script>");

            // }
            // if($monthNum<1 || ($hao>=$setting['txtime1'] && $hao<=$setting['txtime2'])){
            //     die("<script>alert('直接提现钱包要注册激活之后下一个月开始，每个月的".$setting['txtime1']."~".$setting['txtime2']."号才能提现!');history.back(-1);</script>");

            // }
            if($hao<$setting['txtime1'] || $hao>$setting['txtime2']){

                die("<script>alert('直接提现钱包要每个月的".$setting['txtime1']."~".$setting['txtime2']."号才能提现!');history.back(-1);</script>");

            }
            // echo $hao.'<br>';
            //     echo $setting['txtime1'].'<br>';
            //     echo $setting['txtime2'].'<br>';
            //     die;
            

            if($map['price']>$_SESSION['ktx_fh']){
                die("<script>alert('直接提现钱包可提现金额不足,提现失败!');history.back(-1);</script>");
            }
            if($map['price']>$account['cashmoney']){
                die("<script>alert('直接提现钱包余额不足,提现失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('cashmoney',$map['price']);

        }
        if($map['txpay']==2){

            if(($map['price']<$setting['txmoney']) || ($map['price'] % $setting['txbei'] > 0)){
                die("<script>alert('提现金额".$setting['txmoney']."起提，且是".$setting['txbei']."的倍数!');history.back(-1);</script>");
            }

            if($map['price']>$account['halfmoney']){
                die("<script>alert('季度分红钱包余额不足,提现失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('halfmoney',$map['price']);

        }
        if($map['txpay']==3){

            if(($map['price']<$setting['txmoney']) || ($map['price'] % $setting['txbei'] > 0)){
                die("<script>alert('半年分红钱包提现金额".$setting['txmoney']."起提，且是".$setting['txbei']."的倍数!');history.back(-1);</script>");
            }

            if($monthNum<=6){
                die("<script>alert('半年分红钱包要注册激活之后6个月才能提现!');history.back(-1);</script>");

            }

            if($map['price']>$_SESSION['ktx_year']){
                die("<script>alert('半年分红钱包可提现金额不足,提现失败!');history.back(-1);</script>");
            }

            if($map['price']>$account['yearmoney']){
                die("<script>alert('半年分红钱包余额不足,提现失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('yearmoney',$map['price']);

        }
        if($map['txpay']==4){

            if(($map['price']<$setting['zt_txmoney']) || ($map['price'] % $setting['zt_txbei'] > 0)){
                die("<script>alert('直推奖钱包提现金额".$setting['zt_txmoney']."起提，且是".$setting['zt_txbei']."的倍数!');history.back(-1);</script>");
            }

            if($map['price']>$account['zhitui']){
                die("<script>alert('直推奖钱包余额不足,提现失败!');history.back(-1);</script>");
            }
            $reg=M('account')->where(array('id' => $_SESSION['userid']))->setDec('zhitui',$map['price']);

        }

        $map['sxfmoney']=$map['price']*0.05;
        $map['shjmoney']=$map['price']*0.95;
        $map['txtime']=time();
        $map['status']=0;


        M('tixing')->add($map);//生成提现记录

        if($reg){
            echo"<script>alert('提现申请提交成功，等待审核!');location.href='/user/txgl.html'</script>";
            
        }else{
            
            echo"<script>alert('提现申请提交失败!');location.href='/user/txgl.html'</script>";
        }
        
        
    }

    /**
    * @author injection(injection.mail@gmail.com)
    * @var date1日期1
    * @var date2 日期2
    * @var tags 年月日之间的分隔符标记,默认为'-' 
    * @return 相差的月份数量
    * @example:*/
    private function getMonthNum( $date1, $date2, $tags='-' ){

        $date1 = explode($tags,$date1);
        $date2 = explode($tags,$date2);
        return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);
    }



    public function xyhk(){

        $map['userid']=$_SESSION['userid'];

        $num = 5;//默认每頁顯示的記錄數

        $count = M('bank')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('bank')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數


        $this->display();
    }

    public function bank_add(){

        $data['bank_username'] = I('post.bank_username');
        if(empty($data['bank_username'])){

            die("<script>alert('请填写开户姓名!');history.back(-1);</script>");

        }
        $data['bank_num'] = I('post.bank_num');
        if(empty($data['bank_num'])){

            die("<script>alert('请填写银行卡号!');history.back(-1);</script>");

        }
        if (!preg_match ( ' /^(\d{16}|\d{19})$/',$data['bank_num'] )){

            die("<script>alert('银行卡号格式不对!');history.back(-1);</script>");
        }
        $data['bank_name'] = I('post.bank_name');
        if(empty($data['bank_name'])){

            die("<script>alert('请填写开户银行!');history.back(-1);</script>");

        }
        $data['bank_zhi'] = I('post.bank_zhi');
        if(empty($data['bank_zhi'])){

            die("<script>alert('请填写支行信息!');history.back(-1);</script>");

        }

        $data['userid'] = $_SESSION['userid'];

        
        $reg=M('bank')->add($data);//添加新的银行账户

        if($reg){
            echo"<script>alert('添加成功!');location.href='/user/xyhk.html'</script>";
            
        }else{
            
            echo"<script>alert('添加失败!');location.href='/user/xyhk.html'</script>";
        }
        
    }

    public function bank_del(){

        $data = I('get.id');
        if ($data <> '') {
            $bank = M('bank')->where(array('id' => $data))->find();
            if($bank){
                
                $reg=M('bank')->where(array('id' => $data))->delete();
        
                // $this->success('删除成功!');
                echo"<script>alert('删除成功!');location.href='/user/xyhk.html'</script>";
            } else {
                // $this->success('不存在此提现记录!');
                echo"<script>alert('删除失败!');location.href='/user/xyhk.html'</script>";
            }

        }  


        
    }

    public function xwgg(){

        
        $user=M('account')->where(array('id' => $_SESSION['userid']))->find();
        if($user['newsid']<>''){

            $news=M('news')->where(array('id' => $user['newsid']))->find();

            $this->assign('news', $news);

            $map['id']=array('neq',$user['newsid']);

        }

        $num = 5;//默认每頁顯示的記錄數

        $count = M('news')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('news')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數


        $this->display();
    }

    public function xwxq(){

        $id = I('get.id');
        $news = M('news')->where(array('id' => $id))->find();

        $this->assign('news', $news);


        $this->display();
    }

    public function xw_zhd(){

        $data['newsid'] = I('get.id');
        
        $reg=M('account')->where(array('id' => $_SESSION['userid']))->save($data);

        if($reg){
            echo"<script>alert('置顶成功!');location.href='/user/xwgg.html'</script>";
            
        }else{
            
            echo"<script>alert('置顶失败!');location.href='/user/xwgg.html'</script>";
        }


        
    }

    public function wyly(){


        $this->display();
    }

    public function ly_form(){

        $data['title'] = I('post.title');
        if(empty($data['title'])){

            die("<script>alert('请填写标题!');history.back(-1);</script>");

        }
        $data['phone'] = I('post.phone');
        if(empty($data['phone'])){

            die("<script>alert('请填写手机号码!');history.back(-1);</script>");

        }
        if (I('post.phone')) {
            //正则表达式匹配
            if (!preg_match ( ' /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))\\d{8})$/',I('post.phone') )) {
                die("<script>alert('手机格式不对!');history.back(-1);</script>");
            }
        }
        $data['sjrname'] = I('post.sjr');
        if(empty($data['sjrname'])){

            die("<script>alert('请填写收信人类型!');history.back(-1);</script>");

        }
        $data['content'] = I('post.content');
        if(empty($data['content'])){

            die("<script>alert('请填写留言内容!');history.back(-1);</script>");

        }

        $data['userid'] = $_SESSION['userid'];
        $data['time'] = time();
        $data['status'] = 0;//留言状态 0未回复状态 1已回复状态

        
        $reg=M('message')->add($data);//生成留言记录

        if($reg){
            echo"<script>alert('留言成功!');location.href='/user/lyxq.html'</script>";
            
        }else{
            
            echo"<script>alert('留言失败!');location.href='/user/wyly.html'</script>";
        }


    }

    public function lyxq(){

        $map['userid']=$_SESSION['userid'];
        $map['status']=0;

        $num = 5;//默认每頁顯示的記錄數

        $count = M('message')->where($map)->count(); // 查詢滿足要求的總記錄數
        //$page = new \Think\Page ( $count, 3 ); // 實例化分頁類 傳入總記錄數和每頁顯示的記錄數(25)

        $p = getpage($count, $num);

        $list = M('message')->where($map)->order('id DESC')->limit($p->firstRow, $p->listRows)->select();

        $this->assign('list', $list); // 賦值數據集
        $this->assign('page', $p->show()); // 賦值分頁輸出
        $this->assign('count',$count);//總記錄數
        $this->num=$num;//每頁顯示的記錄數

        $map['status']=1;

        $count2 = M('message')->where($map)->count(); 

        $p2 = getpage($count2, $num);

        $list2 = M('message')->where($map)->order('id DESC')->limit($p2->firstRow, $p2->listRows)->select();

        $this->assign('list2', $list2); // 賦值數據集
        $this->assign('page2', $p2->show()); // 賦值分頁輸出
        $this->assign('count2',$count2);//總記錄數
        // $this->num=$num;//每頁顯示的記錄數


        $this->display();
    }

    public function xq(){

        $id = I('get.id');
        $message = M('message')->where(array('id' => $id))->find();

        $this->assign('message', $message);


        $this->display();
    }
    


    //退出
    public function logout()
    {
        session(null);
        echo"<script>location.href='/'</script>";

    }
    
    
    public function top_up() {
    	$active = array('', '注册币', '报单币');
    	if(IS_POST) {
    		if(empty($_POST['secpwd'])) {
    			$this -> error('二级密码不能为空');
    			exit;
    		}
	        $user = M('account')->where(array('id' => $_SESSION['userid']))->find();
        	if(!$this->_checkPwd($_POST['secpwd'],$user['secpwd'])){
        		$this -> error('二级密码错误');
    			exit;
        	}

    		if(empty($_POST['log_num'])) {
    			$this -> error('数量不能为空');
    		}
    		$data = array(
    			'log_type' => $_POST['log_type'],
    			'log_active' => 0,
    			'log_abstract' => $user['account'] . '用户充值' . $active[$_POST['log_type']],
    			'log_date' => time(),
    			'log_num' => $_POST['log_num'],
    			'account_id' => $_SESSION['userid'],
    			'log_old_num' => $user['investmoney'],
    			'log_new_num' => $user['investmoney'] + $_POST['log_num'],
    			'log_is_state' => 0,
    			'user_bank_info' => json_encode(array('bank_num' => $user['bank_num'], 'bank_name' => $user['bank_name'], 'bank_username' => $user['bank_username'], 'bank_zhi' => $user['bank_zhi'])),
    			'sys_bank_info' => json_encode(M('bank') -> where('id = ' . $_POST['bank_id']) -> find()),
    			'log_remark' => ' '
    		);
    		if(M('top_up_log') -> add($data)) {
    			$this -> success('充值成功', '/user/top_up');
    			exit;
    		}
    		$this -> error('充值失败');
    		exit;
    	}

		$logs = M('top_up_log') -> where('account_id = ' . $_SESSION['userid']);

		$p = getpage($logs -> count(), 5);

		$this -> assign('active', $active);
		$this -> assign('status', array('否','是'));
		$this -> assign('logs', $logs ->limit($p -> firstRow, $p -> listRows) -> order('log_is_state ASC') -> select());
		$this -> assign('page', $p->show());
    	$this -> display();
    }

}